package ops.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import ops.WindowManager;
import ops.model.EntityAlreadyExistsException;
import ops.model.Subject;
import ops.model.Teacher;
import ops.model.manager.ClassManager;
import ops.model.manager.EntityController;
import ops.model.manager.SubjectManager;
import ops.model.manager.TeacherManager;
import ops.view.ClassRegisterPanel;

public class ClassController {
	
	private ClassManager classManager;	
	
	private boolean adding;

	public ClassController() {
		classManager = EntityController.getInstance().getClassManager();
	}

	public void startAddProcess() {
		adding = true;
	}
	
	public void endAddProcess() {
		adding = false;
	}
	
	private void add(int subject, int size, int term, long teacher) {
		Integer code = classManager.nextIntID();
		
		ops.model.Class clazz = new ops.model.Class();
		clazz.setCode(code);
		clazz.setSubject(subject);
		clazz.setSize(size);
		clazz.setTerm(term);
		clazz.setStudentsGrade(new TreeMap<Long, Double>());
		clazz.setTeacher(teacher);
		
		try {
			classManager.add(clazz);
			classManager.save();
		} catch (EntityAlreadyExistsException e) {
			e.printStackTrace();
		}
	}
	
	private void edit(int code, int subject, int size, int term, long teacher) {
		ops.model.Class clazz = classManager.getObjectByKey(code);		
		clazz.setSubject(subject);
		clazz.setSize(size);
		clazz.setTerm(term);
		clazz.setTeacher(teacher);
		
		classManager.save();
	}
	
	public void saveClass(int code, int subject, int size, int term, long teacher) {
		if (adding) {
			add(subject, size, term, teacher);
			
			adding = false;
		} else {
			edit(code, subject, size, term, teacher);
		}
		
		updateTable();
	}
	
	public void removeClass(int code) {
		classManager.remove(code);
		classManager.save();
		
		updateTable();
	}
	
	public void updateTable() {
		ClassRegisterPanel panel = getPanel();
		
		panel.clearTable();
		
		for (ops.model.Class clazz : classManager.getMap().values()) {
			panel.addRowToTable(clazz.getCode(), getSubjectNameById(clazz.getSubject()), 
					clazz.getSize(), ControllerManager.getInstance().getTermController().getTermNameById(clazz.getTerm()),
					getTeacherNameById(clazz.getTeacher()));
		}
	}
	
	public Map<String, Object> getSubjectsList() {
		SubjectManager subjectManager = EntityController.getInstance().getSubjectManager();
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		for (Subject subject : subjectManager.getMap().values()) {
			map.put(subject.getName(), subject.getCode());
		}
		
		return map;
	}
	
	public String getSubjectNameById(int code) { 
		SubjectManager subjectManager = EntityController.getInstance().getSubjectManager();
		
		return subjectManager.getObjectByKey(code).getName();
	}
	
	public Map<String, Object> getTeachersList() {
		TeacherManager teacherManager = EntityController.getInstance().getTeacherManager();
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		for (Teacher teacher: teacherManager.getMap().values()) {
			map.put(teacher.getName(), teacher.getCode());
		}
		
		return map;
	}
	
	public String getTeacherNameById(long code) { 
		TeacherManager teacherManager = EntityController.getInstance().getTeacherManager();
		
		return teacherManager.getObjectByKey(code).getName();
	}
	
	private ClassRegisterPanel getPanel() {
		return (ClassRegisterPanel) WindowManager.getInstance().getPanel("register.classes");
	}
}
